package com.hz.sdka.sdka;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.ATNetworkConfirmInfo;
import com.anythink.core.api.AdError;
import com.anythink.interstitial.api.ATInterstitial;
import com.anythink.interstitial.api.ATInterstitialExListener;
import com.hz.sdka.commona.api.interstitial.HZInterstitial;
import com.hz.sdka.commona.api.interstitial.OnHZInterstitialListener;
import com.hz.sdka.commona.base.BaseAd;
import com.hz.sdka.commona.bean.HZAdError;
import com.hz.sdka.commona.type.HZAdType;
import com.hz.sdka.commona.utils.LogUtils;

public final class AdInterstitiala extends BaseAd implements HZInterstitial {

    private String TAG = "[interstitial] ===>";

    private ATInterstitial mATInterstitial;

    private OnHZInterstitialListener mOnHZInterstitialListener;

    public AdInterstitiala(Context activity, String placeId) {
        super(placeId, HZAdType.INTERSTITIAL.indexOf());
        this.mATInterstitial = new ATInterstitial(activity, placeId);
        this.mATInterstitial.setAdListener(new ATInterstitialExListener() {
            @Override
            public void onInterstitialAdLoaded() {
                LogUtils.d(TAG, "[onInterstitialAdLoaded]");
                if (mOnHZInterstitialListener != null) {
                    mOnHZInterstitialListener.onInterstitialLoaded();
                }
            }

            @Override
            public void onInterstitialAdLoadFail(AdError adError) {
                LogUtils.d(TAG, "[onInterstitialAdLoadFail]" + adError.getPlatformCode() + adError.getPlatformMSG());
                if (mOnHZInterstitialListener != null) {
                    mOnHZInterstitialListener.onInterstitialLoadFailed(new HZAdError(adError.getCode(),
                            adError.getDesc(),
                            adError.getPlatformCode(),
                            adError.getPlatformMSG()));
                }
            }

            @Override
            public void onInterstitialAdClicked(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onInterstitialAdClicked]");
                if (mOnHZInterstitialListener != null) {
                    mOnHZInterstitialListener.onInterstitialClicked();
                }
            }

            @Override
            public void onInterstitialAdShow(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onInterstitialAdShow]");
                if (mOnHZInterstitialListener != null) {
                    hzAdInfo.setEcpm(String.valueOf(atAdInfo.getEcpm()));
                    hzAdInfo.setNetworkPlacementId(atAdInfo.getNetworkPlacementId());
                    hzAdInfo.setNetworkType(atAdInfo.getAdNetworkType());
                    hzAdInfo.setShowId(atAdInfo.getShowId());
                    mOnHZInterstitialListener.onInterstitialShown(hzAdInfo);
                }
            }

            @Override
            public void onInterstitialAdClose(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onInterstitialAdClose]");
                if (mOnHZInterstitialListener != null) {
                    mOnHZInterstitialListener.onInterstitialClosed();
                }
            }

            @Override
            public void onInterstitialAdVideoStart(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onInterstitialAdVideoStart]");
            }

            @Override
            public void onInterstitialAdVideoEnd(ATAdInfo atAdInfo) {
                LogUtils.d(TAG, "[onInterstitialAdVideoEnd]");
            }

            @Override
            public void onInterstitialAdVideoError(AdError adError) {
                LogUtils.d(TAG, "[onInterstitialAdVideoError]");
            }

            @Override
            public void onDeeplinkCallback(ATAdInfo atAdInfo, boolean b) {
                LogUtils.d(TAG, "[onDeeplinkCallback]");
            }

            @Override
            public void onDownloadConfirm(Context context, ATAdInfo atAdInfo, ATNetworkConfirmInfo atNetworkConfirmInfo) {
                LogUtils.d(TAG, "[onDownloadConfirm]");
            }
        });
    }


    @Override
    public void load() {
        mATInterstitial.load();
    }


    @Override
    public void setListener(OnHZInterstitialListener listener) {
        this.mOnHZInterstitialListener = listener;
    }


    @Override
    public void show(Activity mActivity, String scenarioId) {
        if (mActivity == null || mActivity.isDestroyed() || mActivity.isFinishing()) {
            if (mOnHZInterstitialListener != null) {
                mOnHZInterstitialListener.onInterstitialShowFailed(HZAdError.ACTIVITY_IS_INVALID);
            }
            return;
        }
        if (!TextUtils.isEmpty(scenarioId)) {
            mATInterstitial.show(mActivity, scenarioId);
        } else {
            mATInterstitial.show(mActivity);
        }
    }

    /**
     * 获取广告价格
     */
    public double getEcpm() {
        if (mATInterstitial != null && mATInterstitial.isAdReady()) {
            return mATInterstitial.checkAdStatus().getATTopAdInfo().getEcpm();
        }
        return 0.00;
    }

    @Override
    public boolean isReady() {
        if (mATInterstitial != null)
            return mATInterstitial.isAdReady();
        else return false;
    }

}
